//
//  HGWebUtil.m
//  HGMM
//
//  Created by 2008-bj5 on 15/8/4.
//  Copyright (c) 2015年 cc. All rights reserved.
//

#import "HGWebUtil.h"
//#import "PlayerController.h"
#import "HGPluginViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import "UIView+ViewController.h"

@interface HGWebUtil ()

@property(nonatomic,strong)MPMoviePlayerViewController  *xmoviePlayer;

@end


@implementation HGWebUtil

- (void)callInnerPlayer:(CDVInvokedUrlCommand*)command{
    CCLog(@"HGWebUtil - callInnerPlayer");
    
    if ([command.arguments count] != 1) {
        CCLog(@"HGWebUtil - callInnerPlayer: ERROR");
    }
    
    id tmp = [command.arguments objectAtIndex:0];
    
    if (![tmp isKindOfClass:[NSString class]]) {
        CDVPluginResult* pluginResult = nil;
        
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"参数不正确！"];
        
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        return ;
    }
    
    NSString* param = tmp;
    
    CCLog(@"HGWebUtil callInnerPlayer: %@", param);
    
    HGPluginViewController *vc;
    if ([self.viewController isKindOfClass:[HGPluginViewController class]]) {
        vc = (HGPluginViewController *)self.viewController;
        /*
        if (!vc.rootViewController) {
            CCLog(@"HGWebUtil error2: not rootViewController!");
            return ;
        }
         */
        dispatch_async(dispatch_get_main_queue(), ^{
            /*
            PlayerController *pc = [[PlayerController alloc] init];
            pc.videoUrl = param;
            [vc.rootViewController presentViewController:pc animated:YES completion:^{
                
            }];
             */
            
            NSURL *urlx = [NSURL URLWithString:param];
            
            MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc]initWithContentURL:urlx];
            
            [self.webView.viewContoller presentViewController:mp animated:YES completion:^{
                
                CDVPluginResult* pluginResult = nil;
                
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@""];
                
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
                
                
            }];
            
        
        });
    } else {
        CCLog(@"HGWebUtil error: not HGPluginViewController!");
    }
    
}

@end
